package Freight

import (
	session2 "OnlineRetailers/controllers/admin/session"
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/controllers/login"
	"OnlineRetailers/models/Admin"
	"OnlineRetailers/models/Admin/resMes"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type GetFreight struct {
	beego.Controller
}

func (c *GetFreight) GetFreight() {
	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/Freight/GetFreight"
	res := resMes.GetFreight{}
	freight := []*Admin.Freight{}
	log := &error2.Log{}
	//mes := &error2.Mes{}
	var err error
	o := orm.NewOrm()

	err = session2.CheckSession(c.Ctx.Request.Header["Session"], 0)
	if err != nil && err.Error() == "session == nil" {
		err, _ := login.CheckOpenid(c.Ctx.Request.Header["Openid"])
		if err != nil {
			c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
			return
		}
	} else if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())

		return
	}

	//sql := "select * from `activity`,`classification` Limit ?,?"
	//_, err = o.Raw(sql, begin, begin+models.DateLength).QueryRows(&activitys)
	_, err = o.QueryTable("Freight").All(&freight)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	res.Freight = freight
	res.Code = "200"
	c.Data["json"] = res

}
